package com.eyzhs.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.app.application.MyApplication;
import com.app.utils.Setting;
import com.app.utils.UpdateManager;
import com.app.utils.Utils;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.LogUtilBase;
import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;
import com.lepu.app.main.activity.AppStartActivity;
import com.lepu.app.main.activity.MainTabActivity;
import com.lepu.app.main.push.BeanPush;

public class PushGeTuiReceiver extends BroadcastReceiver {

    private static final String TAG = "PushGeTuiReceiver";
    /**
     * 应用未启动, 个推 service已经被唤醒,保存在该时间段内离线消息(此时 GetuiSdkDemoActivity.tLogView == null)
     */

    @Override
    public void onReceive(final Context context, Intent dataIntent) {
        Bundle bundle = dataIntent.getExtras();
        LogUtilBase.LogD(TAG, "onReceive() action=" + bundle.getInt("action"));
        //LogUtilBase.LogD(TAG, "onReceive() data=" + bundle.toString());

        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_MSG_DATA:
                // 获取透传数据
                // String appid = bundle.getString("appid");
                byte[] payload = bundle.getByteArray("payload");

                String taskid = bundle.getString("taskid");
                String messageid = bundle.getString("messageid");

                // smartPush第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
                boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
                LogUtilBase.LogD(TAG, "第三方回执接口调用" + (result ? "成功" : "失败"));

                if (payload != null) {
                    String data = new String(payload);
                    LogUtilBase.LogD(TAG, "data====>>" + data);

                    //收到透传的消息是data
                    //点击通知事件成功后，收到的data

                    final BeanPush beanPush = BeanPush.parseJsonSimpleData(data);
                    if(beanPush != null)
                    {
                        int delayTime = 0;
                        if (MainTabActivity.getInstance() == null) {
                            // 程序未启动，收到推送
                            delayTime = AppStartActivity.DELAY_TIME * 2;

                            Intent intent = new Intent();
                            intent.setClass(context, AppStartActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                        } else {
                            // 程序已经启动
                            delayTime = 1500;

                            Intent intent = new Intent();
                            intent.setClass(context, MainTabActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                        }


                        new Handler().postDelayed(new Runnable() {

                            @Override
                            public void run() {

                                Utils.clickPush(context, beanPush);
                            }
                        }, delayTime);
                    }
                }
                break;

            case PushConsts.GET_CLIENTID:
                // 获取ClientID(CID)
                // 第三方应用需要将CID上传到第三方服务器，并且将当前用户帐号和CID进行关联，以便日后通过用户帐号查找CID进行消息推送
                String cid = bundle.getString("clientid");
                MyApplication.getInstance().mGeTuiChannelId = cid;
                MyApplication.getInstance().mPushGetSuccessGeTui = true;

                LogUtilBase.LogD(TAG,"getui_channel_id====>>"+cid);

                if(MyApplication.getInstance().mPushGetSuccessBaidu)
                {
                    LogUtilBase.LogD(TAG,"检查更新");
                    // 检查更新
                    BaseFragmentActivity currentActivity = (BaseFragmentActivity) AppManager.getAppManager().
                            currentActivity();
                    UpdateManager.getInstance().checkUpdate(true, Setting.getAppStartUp(),
                            currentActivity, false, false);
                }
                break;
            case PushConsts.GET_SDKONLINESTATE:
                boolean online = bundle.getBoolean("onlineState");
                LogUtilBase.LogD(TAG, "online = " + online);
                break;

            case PushConsts.SET_TAG_RESULT:
                LogUtilBase.LogD(TAG, "PushConsts.SET_TAG_RESULT");

                break;
            case PushConsts.THIRDPART_FEEDBACK:
                LogUtilBase.LogD(TAG, "PushConsts.THIRDPART_FEEDBACK");

                break;

            default:
                break;
        }
    }
}
